package com.xigua.JucOfBeautiful.chapterFour._4_1;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/1/2921:33
 */
public class Atomic {

    private static AtomicLong atomicLong = new AtomicLong();

    private static Integer[] array1 = new Integer[]{0,1,2,3,0,4,5,6,0,5,6,0};
    private static Integer[] array2 = new Integer[]{0,1,2,3,0,4,5,6,0,5,6,0};

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < array1.length; i++) {
                if (array1[i] == 0 ) atomicLong.incrementAndGet();
            }
        });

        Thread thread2 = new Thread(()->{
            for (int i = 0; i < array2.length; i++) {
                if (array2[i] == 0) atomicLong.incrementAndGet();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(atomicLong);

    }

}
